package com.gzu.ltms.uitl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import lombok.Cleanup;

import java.time.LocalDateTime;

public class CodegenUtils {
    // 资源目录
    private static final String SOURCE_DIR = "D:\\Desktop\\logistics_tracking_management_system";
    // 根包路径
    private static final String BASE_PACKAGE = "com.gzu.ltms";
    // 表名前缀
    private static final String TABLE_PREFIX = "";
    // 数据库名
    private static final String DATA_BASE = "logistics_tracking";
    // 数据库地址
    private static final String URL = "113.45.195.163:3306";
    // 用户名
    private static final String USER_NAME = "root";
    // 密码
    private static final String PASSWORD = "125814";

    //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
    private static final Integer JAVA_VERSION = 17;

    public static void main(String[] args) {
        @Cleanup DruidDataSource dataSource = new DruidDataSource();

        //配置数据源
        dataSource.setUrl("jdbc:mysql://"+URL+"/"+DATA_BASE+"?useInformationSchema=true&characterEncoding=utf-8");
        dataSource.setUsername(USER_NAME);
        dataSource.setPassword(PASSWORD);

        GlobalConfig config = createGlobalConfig();

        // 生成
        new Generator(dataSource, config).generate();
    }

    public static GlobalConfig createGlobalConfig() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setSourceDir(SOURCE_DIR + "/src/main/java")
                .setBasePackage(BASE_PACKAGE)
                .setMapperXmlPath(SOURCE_DIR + "/src/main/resources/mapper");

        //设置表前缀和只生成哪些表
        globalConfig.setTablePrefix(TABLE_PREFIX);

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
//        globalConfig.setEntitySuperClass(BaseEntity.class);
        globalConfig.setEntityWithLombok(true);
        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(JAVA_VERSION);


        globalConfig.setMapperAnnotation(true);
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setMapperXmlGenerateEnable(true);


        globalConfig.setTableDefGenerateEnable(true);

        globalConfig.setServiceGenerateEnable(true);
        globalConfig.setServiceImplGenerateEnable(true);

        globalConfig.setEntityOverwriteEnable(true);
        globalConfig.setTableDefOverwriteEnable(true);
        globalConfig.setMapperOverwriteEnable(true);
        globalConfig.setMapperXmlOverwriteEnable(true);


        globalConfig.setAuthor("mm");
        globalConfig.setSince(LocalDateTimeUtil.format(
                LocalDateTime.now(),
                "yyyy-MM-dd"));
        return globalConfig;
    }
}
